// pages/classify/classify.js
import {request} from "../../request/request.js";
import moment from "../../utils/moment"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    TabCur: 0,
    scrollLeft:0,
    //全部分类
    categoriesList: [],
    pageNo: 1,
    pageSize: 5,
    //文章数据
    listIndexVo: [],
    loading: false,
    total: 0,
    isMore: false,
    color: [
      '#6739b6',
      '#9c26b0',
      '#0081ff',
      '#e03997',
      '#8799a3',
      '#39b54a',
      '#1cbbb4'
    ]
  },
  tabSelect(e) {
    this.setData({
      pageNo: 1,
      pageSize: 5,
      listIndexVo: [],
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id-1)*60
    })
    //重新获取数据
    this.listIndexVo();
  },
  async listIndexVo() {
    const pageNo = this.data.pageNo;
    const pageSize = this.data.pageSize;
    const categoryId = this.data.categoriesList[this.data.TabCur].id;
    console.log(categoryId);
    this.setData({
      loading: true
    })
    const tagsId = 0
    const res = await request({
      url: '/article/listIndexVo',
      data: {
        pageNo,
        pageSize,
        categoryId,
        tagsId
      }
    })
    this.setData({
      loading: false
    })
    let newListIndexVo = [];
    console.log(res);
    if(res.code === 2000) {
      newListIndexVo = res.data.listIndexVo;
      this.setData({
        total: res.data.total
      })
    }
    console.log(newListIndexVo)
    newListIndexVo.forEach(element => {
      if(element.image === null) {
        element.image = 'http://dd.ddandang.top/blog/images/WeChat/default.png';
      }
      //将时间进行切割
      let time = moment(element.gmtCreate).format("MMMM Do YYYY, h:mm:ss a");
      let times = time.split(",");
      element.time1 = times[0];
      element.time2 = times[1];
    })
    let listIndexVo = this.data.listIndexVo;
    listIndexVo.push(...newListIndexVo);
    this.setData({
      listIndexVo: listIndexVo
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.allCategories();
  },

  async allCategories() {
    const res = await request({
      url: '/categories/allCategories'
    })
    console.log(res);
    if(res.code === 2000) {
      this.setData({
        categoriesList: res.data.categoriesList
      })
      this.listIndexVo();
    }
    
  },
  //查看文章详细
  toArticle(e) {
    let articleId = e.currentTarget.dataset.articleId;
    console.log(articleId);
    wx.navigateTo({
      url: '/pages/article/article?articleId=' + articleId,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let pageNo = this.data.pageNo;
    if(pageNo * this.data.pageSize >= this.data.total) {
      console.log("没有数据了")
      this.setData({
        isMore: true
      })
      return;
    }
    this.setData({
      pageNo: pageNo + 1
    })
    console.log("触底了");
    this.listIndexVo();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})